
local U = require "packages/hanqing/util"

local yinfeng = fk.CreateSkill{
  name = "hanqing__yinfeng&",
  tags = { U.VerticalAlliance },
}

Fk:loadTranslationTable{
  ["hanqing__yinfeng&"] = "引锋",
  [":hanqing__yinfeng&"] = "<a href='hanqing__alliance_skill'>合纵技</a>，"..
  "你可以将武器牌当【杀】、【杀】当【借刀杀人】、【借刀杀人】当任意伤害锦囊牌使用。",
}

yinfeng:addEffect("viewas", {
  anim_type = "offensive",
  pattern = ".",
  prompt = "#hanqing__yinfeng",
  handly_pile = true,
  interaction = function (self, player)
    local all_names = table.filter(Fk:getAllCardNames("t"), function (name)
      return Fk:cloneCard(name).is_damage_card
    end)
    table.insert(all_names, 1, "slash")
    table.insert(all_names, 2, "collateral")
    local names = player:getViewAsCardNames("hanqing__yinfeng", all_names)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 then
      if self.interaction.data == "slash" then
        return Fk:getCardById(to_select).sub_type == Card.SubtypeWeapon
      elseif self.interaction.data == "collateral" then
        return Fk:getCardById(to_select).trueName == "slash"
      else
        return Fk:getCardById(to_select).trueName == "collateral"
      end
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or self.interaction.data == nil then return end
    local card, c = Fk:getCardById(cards[1])
    if card.sub_type == Card.SubtypeWeapon then
      c = Fk:cloneCard("slash")
    elseif card.trueName == "slash" then
      c = Fk:cloneCard("collateral")
    elseif card.trueName == "collateral" then
      c = Fk:cloneCard(self.interaction.data)
    end
    c.skillName = "hanqing__yinfeng"
    c:addSubcards(cards)
    return c
  end,
  enabled_at_play = function (self, player)
    return U.getAlliance(player) == "vertical"
  end,
  enabled_at_response = function (self, player, response)
    if response or U.getAlliance(player) ~= "vertical" then return end
    local all_names = table.filter(Fk:getAllCardNames("t"), function (name)
      return Fk:cloneCard(name).is_damage_card
    end)
    table.insert(all_names, "slash")
    table.insert(all_names, "collateral")
    return #player:getViewAsCardNames("hanqing__yinfeng", all_names) > 0
  end,
  enabled_at_nullification = Util.FalseFunc,
})

return yinfeng
